<template>
  <div class="Users">
    <Breadcrumb></Breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="userObj.query"
            clearable
            @clear="find()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="find()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="dialogFormVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="UserData" border style="width: 100%">
        <el-table-column fixed type="index" label="#" width="50">
        </el-table-column>
        <el-table-column prop="username" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" align="center">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" align="center">
        </el-table-column>
        <el-table-column prop="address" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#cccccc"
              @change="editSwitch(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="role_name" label="角色" align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="EditTan(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="UserDel(scope.row.id)"
              size="mini"
            ></el-button>
            <el-button
              type="info"
              icon="el-icon-message"
              circle
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加 -->
      <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
        <el-form :model="UserForm" :rules="UserRules" ref="UserRef">
          <el-form-item
            label="用户名"
            :label-width="formLabelWidth"
            prop="username"
          >
            <el-input v-model="UserForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            :label-width="formLabelWidth"
            prop="password"
          >
            <el-input v-model="UserForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="UserForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="电话"
            :label-width="formLabelWidth"
            prop="mobile"
          >
            <el-input v-model="UserForm.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog title="编辑用户" :visible.sync="EditDialogFormVisible">
        <el-form :model="EditForm" :rules="EditRules" ref="EditRef">
          <el-form-item
            label="用户名"
            :label-width="formLabelWidth"
            prop="username"
          >
            <el-input
              v-model="EditForm.username"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="EditForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="电话"
            :label-width="formLabelWidth"
            prop="mobile"
          >
            <el-input v-model="EditForm.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="EditDialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="EditUser()">确 定</el-button>
        </div>
      </el-dialog>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userObj.pagenum"
        :page-sizes="[3, 6, 8, 12]"
        :page-size="userObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import Breadcrumb  from "@/mixins/mixin.vue";
import {
  getList,
  getUserAdd,
  getUserDel,
  getUserEdit,
  getSwitch,
} from "@/utils/axios/userApi";
export default {
  components:{Breadcrumb},
  data() {
    var checkAge = (rule, value, cd) => {
      let re = /^1[3-9]\d{9}$/;
      if (!re.test(value)) {
        return cd("请输入正确手机格式");
      } else {
        cd();
      }
    };

    return {
      UserData: [],
      EditForm: {
        username: "",
        email: "",
        mobile: "",
      },
      //正常
      UserRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          //正则
          { validator: checkAge, trigger: ["blur"] },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: ["blur"],
          },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur"],
          },
        ],
      },
      EditRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          //正则
          { validator: checkAge, trigger: "blur" },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: ["blur", "change"],
          },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
      formLabelWidth: "120px",
      dialogFormVisible: false,
      EditDialogFormVisible: false,
      Switch: true,
      UserForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
        id: "",
      },
      userObj: {
        query: "",
        pagenum: 1,
        pagesize: 3,
      },
      total: 0,
    };
  },
  mounted() {
    this.getUsers();
  },
  methods: {
    //查找
    find() {
      this.getUsers();
    },
    handleSizeChange(val) {
      this.userObj.pagesize = val;
      this.getUsers();
    },
    handleCurrentChange(val) {
      this.userObj.pagenum = val;
      this.getUsers();
    },
    //编辑回显
    EditTan(arr) {
      console.log(arr);
      this.EditDialogFormVisible = true;
      this.EditForm = arr;
    },
    //表单修改
    async EditUser() {
      this.$refs.EditRef.validate(async (valid) => {
        if (valid) {
          let { data } = await getUserEdit(this.EditForm.id, this.EditForm);
          if (data.meta.status == 200) {
            this.$message.success("修改成功");
            this.getUsers();
            this.EditDialogFormVisible = false;
          }
        } else {
          this.$message.error(dada.meta.msg);
          return false;
        }
      });
    },
    //创建
    async addUser() {
      this.$refs.UserRef.validate(async (valid) => {
        if (valid) {
          let { data } = await getUserAdd(this.UserForm);
          if (data.meta.status == 201) {
            this.$message.success("创建成功");
            this.UserForm = {
              username: "",
              password: "",
              email: "",
              mobile: "",
            };
            this.dialogFormVisible = false;
            this.getUsers();
          }
        } else {
          this.$message.error(data.meta.msg);
          this.dialogFormVisible = false;
          return false;
        }
      });
    },
    async getUsers() {
      let { data } = await getList(this.userObj);
      console.log(data);
      this.UserData = data.data.users;
      this.total = data.data.total;
    },
    //删除
    async UserDel(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { data } = await getUserDel(id);
          if (data.meta.status == 200) {
            this.$message.success("删除成功");
          } else {
            this.$message.error(data.meta.msg);
          }
          this.getUsers();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async editSwitch(obj) {
      let { data: res } = await getSwitch(obj);
      if (res.meta.status == 200) {
        this.$message.success(res.meta.msg);
      } else {
        this.$message.error(res.meta.msg);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
.el-form {
  width: 90%;
}
</style>